package com.qf.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.service.DocService;
import com.qianfeng.smsplatform.common.model.Standard_Submit;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;

/*
 * @Data: 2021/7/22 21:01
 * */
@Service
public class DocServiceImpl implements DocService {

    @Value("${elasticsearch.index.name}")
    private String index;

    private ObjectMapper objectMapper;
    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
        //TODO 这里单独设置日期格式 而不是直接给目标嘞上面添加jsonformat 注解的原因是
//      因为直接在目标类上面会导致所有依赖目标类的项目都要加Jackson依赖  而我们有些模块是不需要的
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(simpleDateFormat);
    }

    private RestHighLevelClient highLevelClient;
    @Autowired
    public void setHighLevelClient(RestHighLevelClient highLevelClient) {
        this.highLevelClient = highLevelClient;

    }

    @Override
    public void addDoc(Standard_Submit standard_submit) throws IOException {
//        设置要操作的库
        IndexRequest request=new IndexRequest(index);
//        转换成MAP 类型的字符串  JSON
        String json = objectMapper.writeValueAsString(standard_submit);

        request.source(json, XContentType.JSON);
//      添加数据
        highLevelClient.index(request, RequestOptions.DEFAULT);

    }

    @Override
    public void updateDoc(Standard_Submit submit) throws IOException {

        UpdateRequest updateRequest=new UpdateRequest();
//      要修改的库
        updateRequest.index(index);

        Map map = queryDataByCidAndPhoneAndSrcId(submit.getClientID(), submit.getSrcSequenceId(), submit.getDestMobile());
        if(map!=null){
//           获取ID
            Object id = map.get("_id");
            updateRequest.id((String) id);
            updateRequest.doc(objectMapper.writeValueAsString(submit),XContentType.JSON);
            highLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        }



    }

    @Override
    public Map queryDataByCidAndPhoneAndSrcId(int clientId, Long srcId, String phoneNum) throws IOException {
//      要查询的库
        SearchRequest searchRequest=new SearchRequest(index);
//        设置查询方式        MUST 代表3 个条件都得满足
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("clientID", clientId))
                .must(QueryBuilders.termQuery("srcSequenceId", srcId))
                .must(QueryBuilders.termQuery("destMobile", phoneNum)));
        searchRequest.source(searchSourceBuilder);

        SearchResponse search = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = search.getHits().getHits();
        System.out.println("hits----->"+hits);
        if(hits!=null){
            for(SearchHit hit:hits){
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String id = hit.getId();
//                因为sourceAsMap  是不包含ID 的  所以单独获取一下ID 设置进去
                sourceAsMap.put("_id", id);
                System.out.println("sourceAsMap-->"+sourceAsMap);
                return sourceAsMap;
            }

        }
        return null;
    }
}
